package com.ihr360.payroll.service.attendanceplan.impl;

import com.ihr360.payroll.dao.PersonalLeaveRuleRepository;
import com.ihr360.payroll.model.dto.attendance.rule.personalleave.PersonalLeaveRule;
import com.ihr360.payroll.service.attendanceplan.PersonalLeaveRuleService;
import com.ihr360.rest.reposity.SimpleRestReposity;
import com.ihr360.rest.service.AbstractSimpleRestServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @author tomtao
 */
@Service
public class PersonalLeaveRuleServiceImpl extends AbstractSimpleRestServiceImpl<PersonalLeaveRule, Long> implements PersonalLeaveRuleService {

    @Autowired
    PersonalLeaveRuleRepository personalLeaveRuleRepository;

    @Override
    public SimpleRestReposity<PersonalLeaveRule, Long> getSimpleRestReposity() {
        return personalLeaveRuleRepository;
    }

    @Override
    public PersonalLeaveRule save(PersonalLeaveRule personalLeaveRule) {
        if(personalLeaveRule.getId() != null) {
            PersonalLeaveRule ret = personalLeaveRuleRepository.findOne(personalLeaveRule.getId());
            if(ret == null) {
                return null;
            }
            ret.setBasementType(personalLeaveRule.getBasementType());
            ret.setBaseRatio(personalLeaveRule.getBaseRatio());
            ret.setEnableSubsectionPaid(personalLeaveRule.getEnableSubsectionPaid());
            ret.setFixedBase(personalLeaveRule.getFixedBase());
            ret.setNumOpRatio(personalLeaveRule.getNumOpRatio());
            ret.setReferenceFields(personalLeaveRule.getReferenceFields());
            ret.setRemark(personalLeaveRule.getRemark());
            ret.setUpdatedAt(new Date());
            return personalLeaveRuleRepository.save(ret);
        }
        return personalLeaveRuleRepository.save(personalLeaveRule);
    }
}
